//lay truoc tiep  xhttp.responseXML;
xmlDoc=loadXMLDoc("books.xml");

/*xml
<book category="cooking">
	<title lang="en">Everyday Italian</title>
	<author>Giada De Laurentiis</author>
	<year>2005</year>
	<price>30.00</price>
</book>
*/

//lay attribute, xuat ra cooking va 1.
x=xmlDoc.getElementsByTagName("book")[0].attributes; //lay vo ... trong the<book ...>
document.write(x.getNamedItem("category").nodeValue);
document.write("<br />" + x.length);

//lay tat ca the title, xuat ra het the title Everyday Italian,Harry Potter...
x=xmlDoc.getElementsByTagName("title");
//lay tat ca element la title, neu khong dung getElementsByTagName("title")[0] de lay title dau tien.
for (i=0;i<x.length;i++)
  { 
  document.write(x[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
  
 //lay node con dau tien , xuar ra title, neu ko co title thi chuyen sang author
function get_firstChild(n)
{
	y=n.firstChild;
	while (y.nodeType!=1)
	  {
	  y=y.nextSibling;
	  }
	return y;
}
x=get_firstChild(xmlDoc.getElementsByTagName("book")[0]);
document.write(x.nodeName);

//duyet node theo so dau cay
x=xmlDoc.documentElement.childNodes;//lay duoc cac node book, chua vao trong df
for (i=0;i<x.length;i++)
{ 
if (x[i].nodeType==1)
  {//Process only element nodes (type 1),node do ton tai
  document.write(x[i].nodeName);//book la node cap 1, co ten la book, khong co gia tri! vi chi lay dc node book ben ngoai
  document.write("<br />");
  
  y=x[i].childNodes;//vao ... cap the <book>...<book>, lay tat ca cac ten node
  for (j=0;j<y.length;j++)
	{ 
	if (y[j].nodeType==1)
	  {//Process only element nodes (type 1) 
	  document.write(y[j].childNodes[0].nodeValue);//y[i] la cap the <title>...<title>, childNodes[0] de vao trong..., lay gia tri dau tien
	  document.write("<br />");
	  
	  } 
	}
  } 
}

// get node cha, xuat ra node, xuat ra bookstore
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
document.writeln("Parent of Title : "+x.parentNode.nodeName +"<br />");

y=x.parentNode;
ychild=y.childNodes;
document.write("Cac node cung cap voi title<br />");
for (i=0;i<ychild.length;i++)
  { 
	  if (ychild[i].nodeType==1){
		  document.write(ychild[i].nodeName + "_co gia tri la_" +ychild[i].childNodes[0].nodeValue);
		  // document.write(ychild[i].childNodes[0].nodeValue);
		  document.write("<br />");
	  }
  }

//lay toan bo source xml

xmlDoc=loadXMLDoc("books.xml");

//lay node dau tien
root=xmlDoc.documentElement
document.write("<-"+root.nodeName+"-> <br />");

x=root.childNodes;//lay duoc cac node book, chua vao trong df
for (i=0;i<x.length;i++)
{ 
	if (x[i].nodeType==1)
	{//Process only element nodes (type 1),node do ton tai
		//lay attribute ben trong cap the <book category='...' >
		att=x[i].attributes;
		attval=att.getNamedItem("category").nodeValue;
		document.write("__<-"+x[i].nodeName+ " category= '"+attval+ "' -> <br />");//book la node cap 1, co ten la book, khong co gia tri! vi chi lay dc node book ben ngoai   
		 y=x[i].childNodes;//vao ... cap the <book>...<book>, lay tat ca cac ten node
		  for (j=0;j<y.length;j++)
			{ 
				if (y[j].nodeType==1)
				{//Process only element nodes (type 1) 
					 document.write("____<-/"+y[j].nodeName+"->"); //lay ten node hien thoi
					  document.write(y[j].childNodes[0].nodeValue);//y[i] la cap the <title>...<title>, childNodes[0] de vao trong..., lay gia tri dau tien
					  document.write("<-/"+y[j].nodeName+"-> <br />")
				  
				 } 
			}
			 document.write("__<-/"+x[i].nodeName+"-> <br />")
	  } 
}
document.write("<-/"+root.nodeName+"-> <br />");
